package com.fh.shop.admin.biz.brand;

import com.fh.shop.admin.common.ServerResponse;
import com.fh.shop.admin.mapper.brand.IBrandMapper;
import com.fh.shop.admin.po.brand.Brand;
import com.fh.shop.admin.common.DataTableResult;
import com.fh.shop.admin.param.brand.BrandQueryParam;
import com.fh.shop.admin.param.brand.BrandUpdateParam;
import com.fh.shop.admin.util.OSSUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("brandService")
public class IBrandServiceImpl implements IBrandService {
    @Autowired
    private IBrandMapper brandMapper;

    private Logger logger = LoggerFactory.getLogger(IBrandServiceImpl.class);

    //新增
    @Override
    public ServerResponse addBrand(Brand brand) {
        brandMapper.addBrand(brand);
        return ServerResponse.success();
    }

    //列表展示
    @Override
    public DataTableResult findBrandlist(BrandQueryParam brandQueryParam) {
        logger.info("业务逻辑层——查询所有品牌数据");
        Long count = brandMapper.findCount(brandQueryParam);
        List<Brand> brandList = brandMapper.findBrandlist(brandQueryParam);
        return new DataTableResult(brandQueryParam.getDraw(),count,count,brandList);
    }

    //回显
    @Override
    public ServerResponse findBrand(Long id) {
        Brand brand = brandMapper.findBrand(id);
        return ServerResponse.success(brand);
    }

    //修改
    @Override
    public ServerResponse editBrand(BrandUpdateParam brandUpdateParam, String realPath) {
        Brand brand = new Brand();
        brand.setId(brandUpdateParam.getId());
        brand.setBrandName(brandUpdateParam.getBrandName());
        String logo = brandUpdateParam.getLogo();
        String oldLogo = brandUpdateParam.getOldLogo();
        if (StringUtils.isNotEmpty(logo)){
            //放入新图
            brand.setLogo(logo);
            //删除老图
            OSSUtil.deleteFile(oldLogo);

        }else {
            //放入老图
            brand.setLogo(oldLogo);
        }
        brandMapper.editBrand(brand);
        return ServerResponse.success();
    }

    //删除
    @Override
    public ServerResponse deleteBrand(Long id, String realPath) {
        //删除硬盘上的图片
        Brand brand = brandMapper.findBrand(id);
        OSSUtil.deleteFile(brand.getLogo());
        //删除数据库信息
        brandMapper.deleteBrand(id);
        return ServerResponse.success();
    }

    //批量删除
    @Override
    public ServerResponse deleteBatchBrand(String ids, String realPath) {
        if (StringUtils.isNotEmpty(ids)){
            String[] idArr = ids.split(",");
            List<Long> idList = new ArrayList<>();
            for (String s : idArr) {
                idList.add(Long.parseLong(s));
            }
            //删除硬盘上的图片
            List<Brand> brand = brandMapper.findBatchBrand(idList);
            List<String> logPaths = brand.stream().map(x -> x.getLogo()).collect(Collectors.toList());
            OSSUtil.deleteBatchFile(logPaths);

            //删除数据库
            brandMapper.deleteBatchBrand(idList);
        }

        return ServerResponse.success();
    }

    @Override
    public ServerResponse findBrandList() {
        List<Brand> brandList = brandMapper.selectList(null);
        return ServerResponse.success(brandList);
    }
}
